/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/ccjzxyxt">ccjzxyxt</a> All rights reserved.
 */
package cn.net.dckj.modules.biz.web;

import cn.net.dckj.common.config.Global;
import cn.net.dckj.common.persistence.Page;
import cn.net.dckj.common.utils.StringUtils;
import cn.net.dckj.common.web.BaseController;
import cn.net.dckj.modules.biz.entity.BizEcsOrder;
import cn.net.dckj.modules.biz.entity.BizEcsSetting;
import cn.net.dckj.modules.biz.service.BizEcsOrderService;
import cn.net.dckj.modules.biz.service.BizEcsSettingService;
import cn.net.dckj.modules.cms.entity.Site;
import cn.net.dckj.modules.cms.utils.CmsUtils;
import cn.net.dckj.modules.sys.entity.Menu;
import cn.net.dckj.modules.sys.entity.User;
import cn.net.dckj.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 云服务器套餐订单Controller
 *
 * @author hqj
 * @version 2019-04-22
 */
@Controller
@RequestMapping(value = "${adminPath}/biz/bizEcsOrder")
public class BizEcsOrderController extends BaseController {

    @Autowired
    private BizEcsOrderService bizEcsOrderService;
    @Autowired
    private BizEcsSettingService bizEcsSettingService;

    @ModelAttribute
    public BizEcsOrder get(@RequestParam(required = false) String id) {
        BizEcsOrder entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = bizEcsOrderService.get(id);
        }
        if (entity == null) {
            entity = new BizEcsOrder();
        }
        return entity;
    }

    @RequiresPermissions("biz:bizEcsOrder:view")
    @RequestMapping(value = {"list", ""})
    public String list(BizEcsOrder bizEcsOrder, HttpServletRequest request, HttpServletResponse response, Model model) {
        //增加数据权限控制，admin查询所有数据，其他用户只查询本人数据
        User user = UserUtils.getUser();
        if (!user.isAdmin()) {
            if (bizEcsOrder.getCreateBy() == null) {
                bizEcsOrder.setCreateBy(user);
            } else {
                if (StringUtils.isBlank(bizEcsOrder.getCreateBy().getId())) {
                    bizEcsOrder.setCreateBy(user);
                }
            }
        }
        Page<BizEcsOrder> page = bizEcsOrderService.findPage(new Page<BizEcsOrder>(request, response), bizEcsOrder);
        model.addAttribute("page", page);
        String origin = request.getParameter("origin");
        if (("front").equals(origin)) {
            Site site = CmsUtils.getSite(Site.defaultSiteId());
            model.addAttribute("site", site);
            Menu menu = UserUtils.getNavMenu("mymeal");
            model.addAttribute("menu", menu);
            String tabIndex = request.getParameter("tabIndex");
            model.addAttribute("tabIndex", tabIndex);
            return "modules/cms/front/themes/basic/grzx_setMeal";
        } else {
            return "modules/biz/bizEcsOrderList";
        }
    }

    @RequiresPermissions("biz:bizEcsOrder:view")
    @RequestMapping(value = "form")
    public String form(BizEcsOrder bizEcsOrder, Model model) {
        model.addAttribute("bizEcsOrder", bizEcsOrder);
        return "modules/biz/bizEcsOrderForm";
    }

    @RequiresPermissions("biz:bizEcsOrder:edit")
    @RequestMapping(value = "save")
    public String save(BizEcsOrder bizEcsOrder, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, bizEcsOrder)) {
            return form(bizEcsOrder, model);
        }
        bizEcsOrder.setOrderFlag("1"); //订单状态 1 未支付 2 已支付 3 已到期
        User user = UserUtils.getUser();
        bizEcsOrder.setGoodId(user.getId());
        bizEcsOrderService.save(bizEcsOrder);
        String id = bizEcsOrder.getId();
        addMessage(redirectAttributes, "保存云服务器套餐订单成功");
        return "redirect:" + Global.getAdminPath() + "/biz/bizEcsOrder/?repage";
    }

    @RequiresPermissions("biz:bizEcsOrder:edit")
    @RequestMapping(value = "delete")
    public String delete(BizEcsOrder bizEcsOrder, HttpServletRequest request, RedirectAttributes redirectAttributes, Model model) {
        String origin = request.getParameter("origin");
        bizEcsOrderService.delete(bizEcsOrder);
        if (("front").equals(origin)) {
            Site site = CmsUtils.getSite(Site.defaultSiteId());
            model.addAttribute("site", site);
            Menu menu = UserUtils.getNavMenu("mymeal");
            model.addAttribute("menu", menu);
            String tabIndex = request.getParameter("tabIndex");
            model.addAttribute("tabIndex", tabIndex);
            addMessage(redirectAttributes, "删除云服务器套餐订单成功");
            return "redirect:" + Global.getAdminPath() + "/biz/bizEcsOrder/?repage&origin=front&tabIndex=" + tabIndex;
        } else {
            addMessage(redirectAttributes, "删除云服务器套餐订单成功");
            return "redirect:" + Global.getAdminPath() + "/biz/bizEcsOrder/?repage";
        }
    }

    /**
     * 套餐直接购买，保存订单，加载支付页面
     */
    @RequiresPermissions("biz:bizEcsOrder:edit")
    @RequestMapping(value = "orderBuy")
    public String orderBuy(String setMealId, String orderId, Model model, RedirectAttributes redirectAttributes) {
        BizEcsSetting bizEcsSetting = bizEcsSettingService.get(setMealId);
        if (bizEcsSetting == null || StringUtils.isEmpty(bizEcsSetting.getId())) {
            addMessage(redirectAttributes, "购买的套餐无效，请重新选择套餐");
            return "redirect:" + Global.getAdminPath() + "/biz/bizEcsOrder/?repage";
        }
        BizEcsOrder bizEcsOrder = bizEcsOrderService.get(orderId);
        // 解决刷新问题，同一订单刷新不会多次保存
        if (bizEcsOrder == null || StringUtils.isEmpty(bizEcsOrder.getId())) {
            bizEcsOrder = new BizEcsOrder();
            bizEcsOrder.setIsNewRecord(true);
            bizEcsOrder.setId(orderId);
            User user = UserUtils.getUser();
            bizEcsOrder.setGoodId(user.getId());
            bizEcsOrder.setOrderFlag("1");  // 1 未支付 2 已支付 3 已到期
            bizEcsOrder.setSetMealId(bizEcsSetting.getId());
            bizEcsOrder.setMealName(bizEcsSetting.getName());
            bizEcsOrder.setCpuNumber(bizEcsSetting.getCpuNumber());
            bizEcsOrder.setRamSize(bizEcsSetting.getRamSize());
            bizEcsOrder.setStorageSize(bizEcsSetting.getStorageSize());
            bizEcsOrder.setMealUseTime(bizEcsSetting.getUseTime());
            bizEcsOrder.setMealPrice(bizEcsSetting.getPrice());
            bizEcsOrderService.save(bizEcsOrder);
        }
        Site site = CmsUtils.getSite(Site.defaultSiteId());
        model.addAttribute("site", site);
        model.addAttribute("bizEcsOrder", bizEcsOrder);
        if ("1".equals(bizEcsOrder.getOrderFlag())) { // 未支付
            return "modules/cms/front/themes/basic/payment";
        } else { // 其他状态
            return "redirect:" + Global.getAdminPath() + "/biz/bizEcsOrder/?repage&origin=front";
        }
    }

    /**
     * 订单支付，加载支付页面
     */
    @RequiresPermissions("biz:bizEcsOrder:edit")
    @RequestMapping(value = "orderPay")
    public String orderPay(String id, Model model, RedirectAttributes redirectAttributes) {
        BizEcsOrder bizEcsOrder = bizEcsOrderService.get(id);
        if (bizEcsOrder == null || StringUtils.isEmpty(bizEcsOrder.getId())) {
            addMessage(redirectAttributes, "支付的订单无效，请重新选择订单");
            return "redirect:" + Global.getAdminPath() + "/biz/bizEcsOrder/?repage&origin=front";
        }
        Site site = CmsUtils.getSite(Site.defaultSiteId());
        model.addAttribute("site", site);
        model.addAttribute("bizEcsOrder", bizEcsOrder);
        if ("1".equals(bizEcsOrder.getOrderFlag())) { // 未支付
            return "modules/cms/front/themes/basic/payment";
        } else { // 其他状态
            return "redirect:" + Global.getAdminPath() + "/biz/bizEcsOrder/?repage&origin=front";
        }
    }
}